package com.turman.composebook.views

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import com.turman.composebook.R
import com.turman.composebook.ui.theme.backgroundColor
import java.text.DecimalFormat
import kotlin.random.Random

val decimalFormat = DecimalFormat("0.0")

@Composable
fun LazyRowSampleView(modifier: Modifier=Modifier){
    LazyColumn(
        contentPadding = PaddingValues(horizontal = 8.dp, vertical = 8.dp),
        verticalArrangement = Arrangement.SpaceEvenly,
        modifier = Modifier
            .fillMaxHeight()
            .background(backgroundColor),
        content = {

            item {
                Text(
                    color = Color(0xffE53935),
                    fontSize = 24.sp,
                    fontWeight = FontWeight.Bold,
                    text = "Snacks"
                )
            }

            item {
                LazyRow(
                    horizontalArrangement = Arrangement.spacedBy(8.dp),
                    content = {
                        items(snacks.size) { it ->
                            HorizontalSnackCard(snack = snacks[it])
                        }
                    }
                )
            }

            item {
                Text(
                    color = Color(0xff4CAF50),
                    fontSize = 24.sp,
                    fontWeight = FontWeight.Bold,
                    text = "Places"
                )
            }

            item {
                LazyRow(
                    horizontalArrangement = Arrangement.spacedBy(8.dp),
                    content = {
                        items(places.size) { it ->
                            PlaceCard(place = places[it])
                        }
                    }
                )
            }

            item {
                Text(
                    color = Color(0xff4CAF50),
                    fontSize = 24.sp,
                    fontWeight = FontWeight.Bold,
                    text = "Places to Visit"
                )
            }

            item {
                LazyRow(
                    horizontalArrangement = Arrangement.spacedBy(8.dp),
                    content = {
                        items(places.size) { it ->
                            PlacesToBookComponent(place = places[it])
                        }
                    }
                )
            }

        })
}

@Immutable
data class Place(
    val id: Long,
    val description: String,
    @DrawableRes val imgRes: Int,
    val rating: Double = Random.nextDouble(0.0, 10.0),
    val price: Int = Random.nextInt(500)
)

val places = listOf(
    Place(1, "Place1", R.drawable.landscape1),
    Place(2, "Place2", R.drawable.landscape2),
    Place(3, "Place3", R.drawable.landscape3),
    Place(4, "Place4", R.drawable.landscape4),
    Place(5, "Place5", R.drawable.landscape5),
    Place(6, "Place6", R.drawable.landscape6),
    Place(7, "Place7", R.drawable.landscape7),
    Place(8, "Place8", R.drawable.landscape8),
    Place(9, "Place9", R.drawable.landscape9),
    Place(10, "Place10", R.drawable.landscape10)
)

@Composable
fun HorizontalSnackCard(
    modifier: Modifier = Modifier,
    snack: Snack,
    textColor: Color = remember(snack.id) { randomColor() },
) {
    Box(contentAlignment = Alignment.TopEnd) {

        Column {

            Image(
                contentScale = ContentScale.FillBounds,
                modifier = modifier
                    .size(160.dp)
                    .clip(RoundedCornerShape(8.dp))
                    .clickable { },
                painter = rememberAsyncImagePainter(
                    ImageRequest.Builder(LocalContext.current).data(data = snack.imageUrl)
                        .apply(block = fun ImageRequest.Builder.() {
                            placeholder(drawableResId = R.drawable.placeholder)
                        }).build()
                ),
                contentDescription = null
            )

            Spacer(modifier = Modifier.height(4.dp))

            Text(
                modifier = Modifier.padding(2.dp),
                color = textColor,
                fontWeight = FontWeight.Bold,
                text = snack.name
            )
        }

        FavoriteButton(
            modifier = Modifier
                .padding(12.dp),
            color = textColor
        )
    }
}

@Composable
fun PlaceCard(place: Place) {

    val painter = painterResource(id = place.imgRes)
    Box(contentAlignment = Alignment.BottomStart) {
        Surface(
            shadowElevation = 4.dp,
            color = Color.LightGray,
            shape = RoundedCornerShape(8.dp),

            ) {
            Image(
                painter = painter,
                contentScale = ContentScale.FillBounds,
                modifier = Modifier
                    .clickable { }
                    .width(150.dp)
                    .height(200.dp),
                contentDescription = null
            )
        }

        Text(
            modifier = Modifier.padding(16.dp),
            text = place.description,
            color = Color.White,
            fontWeight = FontWeight.Bold,
            fontSize = 16.sp
        )
    }
}

@Composable
fun PlacesToBookComponent(place: Place) {
    Card(
        modifier = Modifier
            .wrapContentWidth(),
        elevation = CardDefaults.cardElevation(defaultElevation = 4.dp),
        shape = RoundedCornerShape(8.dp)
    ) {
        Row(modifier = Modifier.clickable { }) {
            PlaceContent(place, Modifier.width(140.dp))
            ImageContent(place)
        }
    }
}


@Composable
fun PlaceContent(place: Place, modifier: Modifier = Modifier) {

    val rating = place.rating

    val triple = getPlaceProperties(rating)

    val color = triple.first
    val text = triple.second
    val startCount = triple.third

    Column(
        modifier = modifier
            .height(140.dp)
            .padding(8.dp)
    ) {

        // 🔥 This weight pushes price text to bottom
        Column(modifier = Modifier.weight(1f)) {
            Text(
                text = place.description,
                fontWeight = FontWeight.Bold,
                fontSize = 18.sp,
                overflow = TextOverflow.Ellipsis
            )

            Spacer(modifier = Modifier.height(8.dp))

            Row {
                repeat(startCount) {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_baseline_star_12),
                        contentDescription = null,
                    )
                }
            }

            Spacer(modifier = Modifier.height(8.dp))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Surface(
                    shape = RoundedCornerShape(4.dp),
                    color = color

                ) {
                    Text(
                        modifier = Modifier.padding(2.dp),
                        text = decimalFormat.format(rating),
                        fontSize = 12.sp,
                        color = Color.White
                    )
                }
                Spacer(modifier = Modifier.width(4.dp))
                Text(text = text, color = color, fontSize = 12.sp)
            }
        }

        Row {
            // 🔥 This Spacer with weight pushes price tag to most right of this context
            Spacer(modifier = Modifier.weight(1f))
            Text(
                text = "$${place.price}",
                color = Color(0xff4CAF50)
            )
        }
    }
}

@Composable
private fun getPlaceProperties(rating: Double): Triple<Color, String, Int> {
    val triple = if (rating > 8) {
        Triple(Color(0xff4CAF50), "Very good", 5)
    } else if (rating <= 8 && rating > 6) {
        Triple(Color(0xff2196F3), "Good", 4)
    } else if (rating > 4 && rating <= 6) {
        Triple(Color(0xffFFEB3B), "Mediocre", 3)

    } else if (rating > 2 && rating <= 4) {
        Triple(Color(0xffFF9800), "Bad", 2)
    } else {
        Triple(Color(0xffF44336), "Very bad", 1)
    }
    return triple
}

@Composable
fun ImageContent(place: Place) {
    Box(contentAlignment = Alignment.TopEnd, modifier = Modifier.size(140.dp)) {

        val painter = painterResource(id = place.imgRes)

        Image(
            modifier = Modifier.fillMaxSize(),
            contentScale = ContentScale.FillBounds,
            painter = painter,
            contentDescription = null
        )

        Surface(
            shape = CircleShape,
            modifier = Modifier
                .padding(6.dp)
                .size(32.dp),
            color = Color(0x77000000)
        ) {
            FavoriteButton(modifier = Modifier.padding(8.dp))
        }
    }
}
